package com.project.frba.bifront.client.view.charts;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.Checkbox;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.layout.FitLayout;
import com.project.frba.bifront.client.model.BarChartInformation;
import com.project.frba.bifront.client.model.Cube;
import com.project.frba.bifront.client.rpc.ChartServices;
import com.project.frba.bifront.client.rpc.ChartServicesAsync;
import com.project.frba.bifront.client.view.MessageWindow;

public class BarChartConfigurationWindow extends Composite{

	private Window mainWindow = new Window("Configurar Grafico de Barra",390,180, true, false);
	private String descriptorName;
	private Button btAccept = new Button("Aceptar");
	private Button btCancel = new Button("Cancelar");
	private Cube cube;
	private TextField tfTitle = new TextField("Titulo", "title", 250);
	private TextField tfDomain = new TextField("Dominio", "domain", 250);
	private TextField tfRange = new TextField("Rango", "range", 250); 
	private Checkbox cbxIncludeLegend = new Checkbox();
	
	
	
	public BarChartConfigurationWindow(Cube cube, String descriptorName){
		this.cube = cube;
		this.descriptorName = descriptorName;
		
		FormPanel formPanel = new FormPanel();
		formPanel.setPaddings(2);
		formPanel.add(tfTitle);
		formPanel.add(tfDomain);
		formPanel.add(tfRange);
		cbxIncludeLegend.setLabel("Incluir leyenda:");
		formPanel.add(cbxIncludeLegend);
	
		loadActions();
		mainWindow.add(formPanel);
		mainWindow.setLayout(new FitLayout());
		mainWindow.setButtons(new Button[]{btAccept, btCancel});
		initWidget(mainWindow);
		mainWindow.center();
		mainWindow.show();
	}
	
	private void loadActions(){
		btAccept.addListener(new ButtonListenerAdapter(){
			public void onClick(Button button, EventObject e) {
				super.onClick(button, e);
				final BarChartInformation info = new BarChartInformation();
				info.setTitle(tfTitle.getValueAsString());
				info.setIncludeLegend(cbxIncludeLegend.getValue());
				info.setSql(cube.getSql());
				info.setColumnPropertyName(cube.getColumnLeafPropertyName());
				info.setRowPropertyName(cube.getRowLeafPropertyName());
				info.setDomainAxisLabel(tfDomain.getValueAsString());
				info.setRangeAxisLabel(tfRange.getValueAsString());
				info.setDescriptorName(descriptorName);
				
				AsyncCallback<String> chartCallback = new AsyncCallback<String>(){
					public void onSuccess(String result) {
						if(result != null){
							new ChartWindow(result, info.toString());
							mainWindow.close();
						}
					}
					public void onFailure(Throwable caught) {
						new MessageWindow("No se pudo generar el grafico, intente nuevamente.");
						
					}
				};
				ChartServicesAsync chartServices = (ChartServicesAsync)GWT.create(ChartServices.class);
				chartServices.doBarChart(info, chartCallback);
				
			}
		});
		
		btCancel.addListener(new ButtonListenerAdapter(){
			public void onClick(Button button, EventObject e) {
				mainWindow.close();
			}
		});
		
	}

}
